home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tcsh / dist / tc.sched.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-21  |  7.2 KB  |  279 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/tc.sched.c,v 3.6 1991/11/26 04:41:23 christos Exp $ */
  2. /*
  3.  * tc.sched.c: Scheduled command execution
  4.  *
  5.  * Karl Kleinpaste: Computer Consoles Inc. 1984
  6.  */
  7. /*-
  8.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  * 3. All advertising materials mentioning features or use of this software
  20.  *    must display the following acknowledgement:
  21.  *    This product includes software developed by the University of
  22.  *    California, Berkeley and its contributors.
  23.  * 4. Neither the name of the University nor the names of its contributors
  24.  *    may be used to endorse or promote products derived from this software
  25.  *    without specific prior written permission.
  26.  *
  27.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37.  * SUCH DAMAGE.
  38.  */
  39. #include "sh.h"
  40.  
  41. RCSID("$Id: tc.sched.c,v 3.6 1991/11/26 04:41:23 christos Exp $")
  42.  
  43. #include "ed.h"
  44. #include "tc.h"
  45.  
  46. extern int just_signaled;
  47.  
  48. struct sched_event {
  49.     struct sched_event *t_next;
  50.     time_t t_when;
  51.     Char  **t_lex;
  52. };
  53. static struct sched_event *sched_ptr = NULL;
  54.  
  55.  
  56. time_t
  57. sched_next()
  58. {
  59.     if (sched_ptr)
  60.     return (sched_ptr->t_when);
  61.     return ((time_t) - 1);
  62. }
  63.  
  64. /*ARGSUSED*/
  65. void
  66. dosched(v, c)
  67.     register Char **v;
  68.     struct command *c;
  69. {
  70.     register struct sched_event *tp, *tp1, *tp2;
  71.     time_t  cur_time;
  72.     int     count, hours, minutes, dif_hour, dif_min;
  73.     Char   *cp;
  74.     bool    relative;        /* time specified as +hh:mm */
  75.     struct tm *ltp;
  76.     char   *timeline;
  77.     char   *ctime();
  78.  
  79. /* This is a major kludge because of a gcc linker  */
  80. /* Problem.  It may or may not be needed for you   */
  81. #ifdef _MINIX
  82.     char kludge[10];
  83.     extern char *sprintf();
  84.     sprintf(kludge,"kludge");
  85. #endif /* _MINIX */
  86.  
  87.     v++;
  88.     cp = *v++;
  89.     if (cp == NULL) {
  90.     /* print list of scheduled events */
  91.     for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) {
  92.         timeline = ctime(&tp->t_when);
  93.         timeline[16] = '\0';
  94.         xprintf("%6d\t%s\t", count, timeline);
  95.         blkpr(tp->t_lex);
  96.         xprintf("\n");
  97.     }
  98.     return;
  99.     }
  100.  
  101.     if (*cp == '-') {
  102.     /* remove item from list */
  103.     if (!sched_ptr)
  104.         stderror(ERR_NOSCHED);
  105.     if (*v)
  106.         stderror(ERR_SCHEDUSAGE);
  107.     count = atoi(short2str(++cp));
  108.     if (count <= 0)
  109.         stderror(ERR_SCHEDUSAGE);
  110.     tp = sched_ptr;
  111.     tp1 = 0;
  112.     while (--count) {
  113.         if (tp->t_next == 0)
  114.         break;
  115.         else {
  116.         tp1 = tp;
  117.         tp = tp->t_next;
  118.         }
  119.     }
  120.     if (count)
  121.         stderror(ERR_SCHEDEV);
  122.     if (tp1 == 0)
  123.         sched_ptr = tp->t_next;
  124.     else
  125.         tp1->t_next = tp->t_next;
  126.     blkfree(tp->t_lex);
  127.     xfree((ptr_t) tp);
  128.     return;
  129.     }
  130.  
  131.     /* else, add an item to the list */
  132.     if (!*v)
  133.     stderror(ERR_SCHEDCOM);
  134.     relative = 0;
  135.     if (!Isdigit(*cp)) {    /* not abs. time */
  136.     if (*cp != '+')
  137.         stderror(ERR_SCHEDUSAGE);
  138.     cp++, relative++;
  139.     }
  140.     minutes = 0;
  141.     hours = atoi(short2str(cp));
  142.     while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p')
  143.     cp++;
  144.     if (*cp && *cp == ':')
  145.     minutes = atoi(short2str(++cp));
  146.     if ((hours < 0) || (minutes < 0) ||
  147.     (hours > 23) || (minutes > 59))
  148.     stderror(ERR_SCHEDTIME);
  149.     while (*cp && *cp != 'p' && *cp != 'a')
  150.     cp++;
  151.     if (*cp && relative)
  152.     stderror(ERR_SCHEDREL);
  153.     if (*cp == 'p')
  154.     hours += 12;
  155.     (void) time(&cur_time);
  156.     ltp = localtime(&cur_time);
  157.     if (relative) {
  158.     dif_hour = hours;
  159.     dif_min = minutes;
  160.     }
  161.     else {
  162.     if ((dif_hour = hours - ltp->tm_hour) < 0)
  163.         dif_hour += 24;
  164.     if ((dif_min = minutes - ltp->tm_min) < 0) {
  165.         dif_min += 60;
  166.         if ((--dif_hour) < 0)
  167.         dif_hour = 23;
  168.     }
  169.     }
  170.     tp = (struct sched_event *) xcalloc(1, sizeof *tp);
  171.     tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L;
  172.     /* use of tm_sec: get to beginning of minute. */
  173.     if (!sched_ptr || tp->t_when < sched_ptr->t_when) {
  174.     tp->t_next = sched_ptr;
  175.     sched_ptr = tp;
  176.     }
  177.     else {
  178.     tp1 = sched_ptr->t_next;
  179.     tp2 = sched_ptr;
  180.     while (tp1 && tp->t_when >= tp1->t_when) {
  181.         tp2 = tp1;
  182.         tp1 = tp1->t_next;
  183.     }
  184.     tp->t_next = tp1;
  185.     tp2->t_next = tp;
  186.     }
  187.     tp->t_lex = saveblk(v);
  188. }
  189.  
  190. /*
  191.  * Execute scheduled events
  192.  */
  193. void
  194. sched_run()
  195. {
  196.     time_t   cur_time;
  197.     register struct sched_event *tp, *tp1;
  198.     struct wordent cmd, *nextword, *lastword;
  199.     struct command *t;
  200.     Char  **v, *cp;
  201.     extern Char GettingInput;
  202.  
  203. #ifdef BSDSIGS
  204.     sigmask_t omask;
  205.  
  206.     omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
  207. #else
  208.     (void) sighold(SIGINT);
  209. #endif
  210.  
  211.     (void) time(&cur_time);
  212.     tp = sched_ptr;
  213.  
  214.     /* bugfix by: Justin Bur at Universite de Montreal */
  215.     /*
  216.      * this test wouldn't be necessary if this routine were not called before
  217.      * each prompt (in sh.c).  But it is, to catch missed alarms.  Someone
  218.      * ought to fix it all up.  -jbb
  219.      */
  220.     if (!(tp && tp->t_when < cur_time)) {
  221. #ifdef BSDSIGS
  222.     (void) sigsetmask(omask);
  223. #else
  224.     (void) sigrelse(SIGINT);
  225. #endif
  226.     return;
  227.     }
  228.  
  229.     if (GettingInput)
  230.     (void) Cookedmode();
  231.  
  232.     while (tp && tp->t_when < cur_time) {
  233.     if (seterr) {
  234.         xfree((ptr_t) seterr);
  235.         seterr = NULL;
  236.     }
  237.     cmd.word = STRNULL;
  238.     lastword = &cmd;
  239.     v = tp->t_lex;
  240.     for (cp = *v; cp; cp = *++v) {
  241.         nextword = (struct wordent *) xcalloc(1, sizeof cmd);
  242.         nextword->word = Strsave(cp);
  243.         lastword->next = nextword;
  244.         nextword->prev = lastword;
  245.         lastword = nextword;
  246.     }
  247.     lastword->next = &cmd;
  248.     cmd.prev = lastword;
  249.     tp1 = tp;
  250.     sched_ptr = tp = tp1->t_next;    /* looping termination cond: */
  251.     blkfree(tp1->t_lex);    /* straighten out in case of */
  252.     xfree((ptr_t) tp1);    /* command blow-up. */
  253.  
  254.     /* expand aliases like process() does. */
  255.     alias(&cmd);
  256.     /* build a syntax tree for the command. */
  257.     t = syntax(cmd.next, &cmd, 0);
  258.     if (seterr)
  259.         stderror(ERR_OLD);
  260.     /* execute the parse tree. */
  261.     execute(t, -1, NULL, NULL);
  262.     /* done. free the lex list and parse tree. */
  263.     freelex(&cmd), freesyn(t);
  264.     }
  265.     if (GettingInput && !just_signaled) {    /* PWP */
  266.     (void) Rawmode();
  267.     ClearLines();        /* do a real refresh since something may */
  268.     ClearDisp();        /* have printed to the screen */
  269.     Refresh();
  270.     }
  271.     just_signaled = 0;
  272.  
  273. #ifdef BSDSIGS
  274.     (void) sigsetmask(omask);
  275. #else
  276.     (void) sigrelse(SIGINT);
  277. #endif
  278. }
  279.